package com.loopy.homework;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "JudgeServlet", value = "/judge")
public class JudgeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("JudgeServlet被调用");
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");

        if("hspedu".equals(username)&&"123456".equals(pwd)){
            Cookie cookie1 = new Cookie("username", username);
//            Cookie cookie2 = new Cookie("password", pwd);
//            cookie1.setMaxAge(0);
            response.addCookie(cookie1);
            cookie1.setMaxAge(259200);
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.print("<h1>登录OK</h1>");

//            response.addCookie(cookie2);
        }else{
            response.sendRedirect("/cookie/login.html");
        }
    }
}
